import 'package:flutter/cupertino.dart';
import 'package:flutter_blog_manage_study/res/strings.dart';
import 'package:flutter_blog_manage_study/ui/dialog/common_dialog.dart';
import 'package:flutter_blog_manage_study/ui/dialog/loading_dialog.dart';
import 'package:get/get.dart';

/// * 作者: Tao
/// * 日期: 2024/3/20
/// * 时间: 10:41
/// * 描述: get 扩展
extension GetExtension on GetInterface {

  /// 隐藏dialog
  dismiss() {
    if (Get.isDialogOpen != null && Get.isDialogOpen!) {
      Get.back();
    }
  }

  /// 显示dialog
  /// * [text] 显示的文本
  showLoading({String text = ''}) {
    /// 如果显示则先隐藏
    dismiss();
    Get.dialog(
        LoadingDialog(text: text.isEmpty ? Strings.loading.tr : text),
        transitionDuration: const Duration(milliseconds: 500),
        barrierDismissible: false);
  }

  /// 显示公共弹窗
  /// * [title] 标题
  /// * [content] 内容
  /// * [backText] 取消按钮文本
  /// * [nextText] 确定按钮文本
  /// * [backVisible] 是否显示取消按钮
  /// * [nextVisible] 是否显示确定按钮
  /// * [backTap] 取消按钮事件
  /// * [nextTap] 确定按钮事件
  /// * [backKey] 是否可以点击返回键关闭
  showCommonDialog({
    String title = '',
    String content = '',
    String backText = '',
    String nextText = '',
    bool backVisible = true,
    bool nextVisible = true,
    VoidCallback? backTap,
    VoidCallback? nextTap,
    backKey = false,
  }) {
    /// 如果显示则先隐藏
    dismiss();
    Get.dialog(
        CommonDialog(
          title: title,
          content: content,
          backText: backText.isEmpty ? Strings.quit.tr : backText,
          nextText: nextText.isEmpty ? Strings.enter.tr : nextText,
          backVisible: backVisible,
          nextVisible: nextVisible,
          backTap: backTap,
          nextTap: nextTap,
        ),
        transitionDuration: const Duration(milliseconds: 500),
        barrierDismissible: backKey);
  }
}
